package deviceGroupConfig

import (
	"context"
	"devops-super/api/configs/v1"
	"devops-super/internal/model/entity"
	"devops-super/internal/service"
	"github.com/gogf/gf/v2/os/gtime"
)

// GetPageLst 分页获取设备分组配置列表
func (*ControllerV1) GetPageLst(ctx context.Context, req *v1.DeviceGroupConfigGetPageLstReq) (res *v1.DeviceGroupConfigGetPageLstRes, err error) {
	res = &v1.DeviceGroupConfigGetPageLstRes{}
	res.PageLstRes, err = service.DeviceGroupConfig().GetPageLst(ctx, req.PageLstReq)
	return
}

// Add 新增设备分组配置
func (*ControllerV1) Add(ctx context.Context, req *v1.DeviceGroupConfigAddReq) (res *v1.DeviceGroupConfigAddRes, err error) {
	config := &entity.AppDeviceGroupConfig{
		DeviceGroupId: req.DeviceGroupId,
		GroupName:     req.GroupName,
		ConfigKey:     req.ConfigKey,
		ConfigValue:   req.ConfigValue,
		Remark:        req.Remark,
		Range:         req.Range,
		CreatedAt:     gtime.Now(),
	}
	err = service.DeviceGroupConfig().Add(ctx, config)
	return
}

// Upt 更新设备分组配置
func (*ControllerV1) Upt(ctx context.Context, req *v1.DeviceGroupConfigUptReq) (res *v1.DeviceGroupConfigUptRes, err error) {
	config := &entity.AppDeviceGroupConfig{
		Id:            int64(req.Id),
		DeviceGroupId: req.DeviceGroupId,
		GroupName:     req.GroupName,
		ConfigKey:     req.ConfigKey,
		ConfigValue:   req.ConfigValue,
		Remark:        req.Remark,
		Range:         req.Range,
		UpdatedAt:     gtime.Now(),
	}
	err = service.DeviceGroupConfig().Upt(ctx, config)
	return
}

// Del 删除设备分组配置
func (*ControllerV1) Del(ctx context.Context, req *v1.DeviceGroupConfigDelReq) (res *v1.DeviceGroupConfigDelRes, err error) {
	err = service.DeviceGroupConfig().Del(ctx, req.Id)
	return
}

// GetOne 获取单条设备分组配置
func (*ControllerV1) GetOne(ctx context.Context, req *v1.DeviceGroupConfigGetOneReq) (res *v1.DeviceGroupConfigGetOneRes, err error) {
	res1, err := service.DeviceGroupConfig().GetOne(ctx, req.Id)
	if err != nil {
		return
	}
	res = new(v1.DeviceGroupConfigGetOneRes)
	res.Info = res1
	return
}

// GetOptions 获取设备分组配置选项
func (*ControllerV1) GetOptions(ctx context.Context, req *v1.DeviceGroupConfigGetOptionsReq) (res *v1.DeviceGroupConfigGetOptionsRes, err error) {
	res, err = service.DeviceGroupConfig().GetOptions(ctx, req)
	return
}

// GetDeviceGroupGroupConfigs 获取设备分组配置组
func (*ControllerV1) GetDeviceGroupGroupConfigs(ctx context.Context, req *v1.GetDeviceGroupGroupConfigsReq) (res *v1.GetDeviceGroupGroupConfigsRes, err error) {
	return service.DeviceGroupConfig().GetDeviceGroupGroupConfigs(ctx, req)
}

// SetDeviceGroupGroupConfigs 保存设备分组配置组
func (*ControllerV1) SetDeviceGroupGroupConfigs(ctx context.Context, req *v1.SetDeviceGroupGroupConfigsReq) (res *v1.SetDeviceGroupGroupConfigsRes, err error) {
	err = service.DeviceGroupConfig().SetDeviceGroupGroupConfigs(ctx, req)
	return
}